package com.hxrfid.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.entity.SysUser;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.utils.StringUtils;
import com.hxrfid.project.domain.BsUserComment;
import com.hxrfid.project.domain.bo.BsUserCommentBo;
import com.hxrfid.project.domain.vo.BsUserCommentVo;
import com.hxrfid.project.mapper.BsUserCommentMapper;
import com.hxrfid.project.service.IBsUserCommentService;
import com.hxrfid.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户评价Service业务层处理
 *
 * @author hxrfid
 * @date 2025-06-12
 */
@RequiredArgsConstructor
@Service
public class BsUserCommentServiceImpl extends ServiceImpl<BsUserCommentMapper, BsUserComment> implements IBsUserCommentService {

    private final BsUserCommentMapper baseMapper;
    private final SysUserMapper sysUserMapper;

    /**
     * 查询用户评价
     * @param id 主键ID
     * @return 用户评价
     */
    @Override
    public BsUserCommentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户评价列表
     * @param bo 用户评价对象
     * @param pageQuery 主键ID
     * @return 用户评价列表
     */
    @Override
    public TableDataInfo<BsUserCommentVo> queryPageList(BsUserCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsUserComment> lqw = buildQueryWrapper(bo);
        Page<BsUserCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户评价列表
     * @param bo 用户评价对象
     * @return 用户评价列表
     */
    @Override
    public List<BsUserCommentVo> queryList(BsUserCommentBo bo) {
        LambdaQueryWrapper<BsUserComment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
    * 组装查询条件
    * @param bo 用户评价对象
    * @return 查询条件
    */
    private LambdaQueryWrapper<BsUserComment> buildQueryWrapper(BsUserCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsUserComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCommentText()), BsUserComment::getCommentText, bo.getCommentText());
        lqw.eq(StringUtils.isNotBlank(bo.getCommentImages()), BsUserComment::getCommentImages, bo.getCommentImages());
        lqw.eq(bo.getCommentStar() != null, BsUserComment::getCommentStar, bo.getCommentStar());
        lqw.eq(StringUtils.isNotBlank(bo.getAddCommentText()), BsUserComment::getAddCommentText, bo.getAddCommentText());
        lqw.eq(StringUtils.isNotBlank(bo.getAddCommentImages()), BsUserComment::getAddCommentImages, bo.getAddCommentImages());
        lqw.eq(bo.getProductId() != null, BsUserComment::getProductId, bo.getProductId());
        lqw.eq(bo.getUserId() != null, BsUserComment::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), BsUserComment::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), BsUserComment::getAvatar, bo.getAvatar());
        return lqw;
    }

    /**
     * 新增用户评价
     * @param bo 用户评价对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean insertByBo(BsUserCommentBo bo) {
        BsUserComment add = BeanUtil.toBean(bo, BsUserComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户评价
     * @param bo 用户评价对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateByBo(BsUserCommentBo bo) {
        BsUserComment update = BeanUtil.toBean(bo, BsUserComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     * @param entity 用户评价对象
     */
    private void validEntityBeforeSave(BsUserComment entity){
        // 查询用户昵称和头像
        SysUser sysUser = sysUserMapper.selectVoById(entity.getUserId());
        entity.setNickName(sysUser.getNickName());
        entity.setAvatar(sysUser.getAvatar());
    }

    /**
     * 批量删除用户评价
     * @param ids 主键ID的list
     * @param isValid 是否业务校验
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
